package com.kuang.demo01;

/**观察测试线程的状态
 * @author FHL
 */
public class TestStatus {

    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("%%%%%%%%延时%%%%%%%");
        });
        //观察其状态
        Thread.State state = thread.getState();
        System.out.println(state);
        //观察启动后的状态
        thread.start();
        state = thread.getState();
        System.out.println(state);
        //线程不终止时，一直输出其状态
        while (state!=Thread.State.TERMINATED){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //更新线程状态，否则得到的一直是一个值
            state = thread.getState();
            System.out.println(state);

        }
    }
}
